package com.zxx.order.presenter

import com.zqq.baselibrary.ext.excute
import com.zqq.baselibrary.http.rx.BaseSubscriber
import com.zqq.baselibrary.presenter.BasePresenter
import com.zxx.order.presenter.iview.OrderConfirmView
import com.zxx.order.service.OrderService
import com.zxx.order.service.bean.Order
import com.zxx.order.service.bean.OrderGoods
import com.zxx.order.service.bean.ShipAddress
import javax.inject.Inject

/**
 * 订单确认页 Presenter
 * wc
 * @author zqq on 2019/6/29.
 */
class OrderConfirmPresenter @Inject constructor() : BasePresenter<OrderConfirmView>() {

    @Inject
    lateinit var orderService: OrderService

    /**
     * 根据ID获取订单
     */
    fun getOrderById(orderId: Int) {
        if (!checkNetWork()) {
            return
        }
        mView.showLoading()

        orderService.getOrderById(orderId)
                .excute(object : BaseSubscriber<Order>(mView) {
                    override fun onNext(t: Order) {
                        super.onNext(t)
                        mView.onGetOrderByIdResult(t)
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        //测试数据返回
                        var order = getOrder()
                        mView.onGetOrderByIdResult(order)
                    }

                }, lifecycleProvider)
    }

    /**
     * 提交订单
     */
    fun submitOrder(order: Order) {
        if (!checkNetWork()) {
            return
        }
        mView.showLoading()
        orderService.submitOrder(order)
                .excute(object : BaseSubscriber<Boolean>(mView) {
                    override fun onNext(t: Boolean) {
                        mView.onSubmitOrderResult(t)
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        //测试
                        mView.onSubmitOrderResult(true)
                    }
                }, lifecycleProvider)

    }


    //测试数据返回
    private fun getOrder(): Order {
        var shipAddress: ShipAddress = ShipAddress(1, "shipName", "135****2534", "杭州市西湖区", 22)
        var orderGoods: OrderGoods = OrderGoods(11, 123, "商品描述",
                "icon", 199, 5, "XL,黑色", 222)
        var goodsList = mutableListOf<OrderGoods>()
        goodsList.add(orderGoods)
        goodsList.add(orderGoods)
        goodsList.add(orderGoods)

        var order = Order(3, 5, shipAddress, 499, 1, goodsList)

        return order
    }


}